﻿

function getSelectedTableKey() {
    return getSelectedItem($id('tableSelectList'));
}

function clickLoadTable() {
    var tbl = getTable(getSelectedTableKey());
    if (tbl != null) {
        var isOwner = tbl.owner == _jsonUser.UserName;
        $id('tablename_h').innerHTML = tbl.tablename + (isOwner? "" : " (owned by " + tbl.owner + ")");
        $id('tableTextBox').value = tbl.description;
        $id('table_ispublic').checked = tbl.is_public;
        $id('table_ispublic').disabled = !isOwner;
        $id('save_table_button').disabled = !isOwner;
        $id('delete_table_button').disabled = !isOwner;
    }
}

function clickDeleteTable() {
    var tablename = $id('tablename_h').innerHTML;
    if (tablename != "") {
        ajaxDeleteTable(tablename,
        function (json) {
            // should return true if succesful otherwise false
            if (json) {
                // refresh page instead of rebuilding indexes
                location.reload(true);
            }
        }); 
    }
}

function clickSaveTable() {
    var tablename = $id('tablename_h').innerHTML;
    var description = $id('tableTextBox').value;
    var is_public = $id('table_ispublic').checked;
    if (tablename != "") {
        ajaxSaveTable(tablename, description, is_public,
        function (json) {
            // do nothing, its already handled in the ajax call
        });
    }
}
